using System;
using System.Collections.Generic;
using System.Text;
using BerkeleyDB;

namespace msgExchange
{
    public class CHistoryDB
    {
        CBDBWrapper db_ = null;
        public CHistoryDB(string db) { db_ = new CBDBWrapper(db); }

        public void add(string GuestUID, LDataItem ldi)
        {
            string d=db_.GetData(GuestUID);
            if (!string.IsNullOrEmpty(d))
            {
                d += ("\f" + ldi.toString());
            }
            else
            {
                d= ldi.toString();
            }
            db_.SetData(GuestUID, d);
        }

        public List<LDataItem> get(string GuestUID)
        {
            string d = db_.GetData(GuestUID);
            if (string.IsNullOrEmpty(d))
            {
                return null;
            }

            List<LDataItem> l = new List<LDataItem>();
            string[] vpi = d.Split('\f');
            foreach (string e in vpi)
            {
                l.Add(new LDataItem(e));
            }

            return l;
        }

    }

    public class CBDBWrapper: IDisposable
    {
        BTreeDatabaseConfig btreeDBConfig;
        BTreeDatabase btreeDB;
        public CBDBWrapper(string dbName)
        {
            btreeDBConfig = new BTreeDatabaseConfig();
            btreeDBConfig.Duplicates = DuplicatesPolicy.NONE;
            btreeDBConfig.Creation = CreatePolicy.IF_NEEDED; 
            btreeDBConfig.PageSize = 4 * 1024;
            btreeDBConfig.CacheSize = new CacheInfo(0, 1024 * 1024 * 80, 0);
            btreeDB = BTreeDatabase.Open(dbName, btreeDBConfig);
        }
        public void SetData(int id, string data)
        {
            DatabaseEntry k = new DatabaseEntry();
            DatabaseEntry d = new DatabaseEntry();
            k.Data = BitConverter.GetBytes(id);
            d.Data = Encoding.UTF8.GetBytes(data);
            try
            {
                btreeDB.Put(k, d);
            }
            catch (System.AccessViolationException av)
            {
            }
            catch { }
        }
        public void SetData(string id, string data)
        {
            DatabaseEntry k = new DatabaseEntry();
            DatabaseEntry d = new DatabaseEntry();
            k.Data = Encoding.UTF8.GetBytes(id);
            d.Data = Encoding.UTF8.GetBytes(data);
            try
            {
                btreeDB.Put(k, d);
            }
            catch (System.AccessViolationException av)
            {
            }
            catch { }
        }
        public string GetData(int id)
        {
            try
            {          
                DatabaseEntry de = new DatabaseEntry();
                de.Data = BitConverter.GetBytes(id);
                KeyValuePair<DatabaseEntry, DatabaseEntry> pair = btreeDB.Get(de);
                if (pair.Value != null)
                   return Encoding.UTF8.GetString(pair.Value.Data);
            }
            catch { }
            return string.Empty;
        }
        public string GetData(string id)
        {
            try
            {
                DatabaseEntry de = new DatabaseEntry();
                de.Data = Encoding.UTF8.GetBytes(id);
                KeyValuePair<DatabaseEntry, DatabaseEntry> pair = btreeDB.Get(de);
                if (pair.Value != null)
                    return Encoding.UTF8.GetString(pair.Value.Data);
            }
            catch { }
            return string.Empty;
        }
        public string GetDBPath()
        {
            return btreeDB.FileName + btreeDB.DatabaseName;
        }
        public void Close()
        {
            btreeDB.Close();
        }
        public void Dispose()
        {
            if (btreeDB != null) btreeDB.Dispose();
        }

    }

}
